package cn.bdqn.layui.controller;

import cn.bdqn.layui.entity.Comment;
import cn.bdqn.layui.entity.Order;
import cn.bdqn.layui.entity.Product;
import cn.bdqn.layui.entity.User;
import cn.bdqn.layui.mapper.CommentMapper;
import cn.bdqn.layui.service.order.OrderService;
import cn.bdqn.layui.service.product.ProductService;
import cn.bdqn.layui.util.ResponseResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class OrderController {

    @Resource
    RabbitTemplate rabbitTemplate;

    @Resource
    ProductService productService;

    @Resource
    OrderService orderService;

    @Resource
    CommentMapper commentMapper;

    @RequestMapping("/order/create")
    @ResponseBody
    public ResponseResult orderCreate(Integer id, Integer num) {
        //拿库存里面的数据
        List<Product> productList = productService.getAllProductByPage();
        for (Product product : productList) {
            if (product.getId() == id) {
                if (num > product.getProductStack()) {
                    return new ResponseResult(0, "商品库存不够");
                } else {
                    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
                    if (!(authentication instanceof AnonymousAuthenticationToken)) {
                        User user = (User) (UserDetails) authentication.getPrincipal();
                        /*if (principal instanceof UserDetails) {
                            User user = (User) (UserDetails) principal;*/
                        //封装订单信息
                        Order order = new Order();
                        order.setUserId(user.getId());
                        order.setProductNum(num);
                        order.setProductName(product.getProductName());
                        order.setProductPrice(product.getProductPrice());
                        order.setUseraddress(user.getAddress());
                        order.setCreateBy(user.getId());
                        order.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                        order.setProductId(id);
                        orderService.addOrder(order);
                        //修改库存
                        productService.updateStack(order.getProductNum(), order.getProductId());
                        //发送消息
                        rabbitTemplate.convertAndSend("order.direct", "orders", order);

                        return new ResponseResult(0, "购买成功");
                    }
                }
            }
        }
        return new ResponseResult(0, "商品库存不够");
    }


    @RequestMapping("/shop/order")
    public String orderAll() {
        return "/views/order/order.html";
    }

    @RequestMapping("/orderList")
    @ResponseBody
    public String  orderList(String productName, Integer page, Integer limit) {
        List<Order> orderList = new ArrayList<>();
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        int count= 0;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            User user = (User) (UserDetails) authentication.getPrincipal();
            orderList = orderService.findOrderAll(0, user.getId(), productName, (page - 1) * limit, limit);
            count  = orderService.findOrderCount(0, user.getId(), productName);
        }
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "");
        object.put("data", orderList);
        object.put("count", count);
        return object.toJSONString();
    }

    @RequestMapping("/toaddcomment")
    public String toAddComment(Integer id, Model model) {
        Order orderById = orderService.findOrderById(id);
        model.addAttribute("orders", orderById);
        return "/views/order/orderfrom";
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public ResponseResult addComment(Integer id, String content) {
        Comment comment = new Comment();
        comment.setOrderId(id);
        comment.setContent(content);
        int i = commentMapper.insertSelective(comment);
        return new ResponseResult(200, "", i);
    }

    @RequestMapping("/order")
    public String addComment(Integer id,Model model) {
        Order orderById = orderService.findOrderById(id);
        List<Comment> commentList = commentMapper.findAllById(id);
        model.addAttribute("orders", orderById);
        model.addAttribute("commentList", commentList);
        return "/views/order/ordercha";
    }

}
